<template>
  <div id="order" class="container">
    <h3 class="title">
      <span />
      站点管理
    </h3>
    <el-button
      type="primary"
      class="add"
      size="small"
      icon="el-icon-plus"
      style="margin-bottom: 15px"
      @click="goAdd()"
    >
      添加
    </el-button>
    <BouncedDialog
      :title="addForm.id ? '编辑' : '添加'"
      :dialog-show.sync="dialogShow"
      width="50%"
      class="link"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        inline
        class="addForm"
        label-width="80px"
      >
        <el-form-item label="上级站点" prop="parent_id">
          <el-select
            v-model="addForm.parent_id"
            :disabled="addForm.id ? true : false"
            @change="changeParent"
          >
            <el-option label="作为一级站点" :value="0" />
            <el-option
              v-for="item in orderData"
              :key="item.id"
              :label="item.domain + '--' + item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="网站名称" prop="title">
          <el-input v-model="addForm.title" />
        </el-form-item>
        <el-form-item label="网站域名" prop="domain">
          <el-input v-model="addForm.domain" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="addForm.status">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="2" />
          </el-select>
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-table ref="refTable" :data="orderData" row-key="id" :indent="20">
      <el-table-column label="ID" prop="id" width="160px" />
      <el-table-column label="网站名称" prop="title" show-overflow-tooltip />
      <el-table-column label="域名" prop="domain" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="'http://' + scope.row.domain" target="_blank">
            {{ scope.row.domain }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="网站状态" prop="status" width="160px">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="2"
            active-text="正常"
            inactive-text="禁用"
            active-color="#f84311"
            inactive-color="rgba(0,0,0,0.2)"
            @change="disableUser(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="380px">
        <template slot-scope="scope">
          <el-button
            v-loading.fullscreen.lock="loading"
            type="primary"
            class="orange"
            size="mini"
            @click="siteMap(scope.row)"
          >
            生成sitemap
          </el-button>
          <el-button
            v-if="scope.row.parent_id == 0"
            class="blue"
            type="primary"
            size="mini"
            @click="goAdd(scope.row)"
          >
            添加子站点
          </el-button>
          <el-button class="edit" size="mini" @click="editor(scope.row)">
            编辑
          </el-button>
          <el-button class="dele" size="mini" @click="del(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> -->
  </div>
</template>
<script>
import websiteApi from '@/api/website.js'

export default {
  data() {
    return {
      loading: false,
      orderData: [], //
      dialogShow: false,
      addForm: {
        id: '',
        title: '',
        domain: '',
        status: '',
        parent_id: '0'
      },
      rules: {
        title: [
          { required: 'true', message: '请输入网站名称', trigger: 'blur' }
        ],
        domain: [
          { required: 'true', message: '请输入网站域名', trigger: 'blur' }
        ]
      },
      searchForm: {
        title: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {},
      id: '',
      reportUrl: ''
    }
  },
  created() {
    this.getList()
    this.reportUrl = 'http://localhost:9528/#/Link/index'
  },
  mounted() {
    // this.getTikets()
  },
  updated() {},
  methods: {
    // 生成sitemap
    siteMap(row) {
      this.loading = true
      websiteApi.sitemap({ website_id: row.id }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          window.open(res.data)
        } else {
          this.$message.error(res.msg)
        }
        this.loading = false
      })
    },
    // 修改状态
    disableUser(row) {
      const val = {
        id: row.id,
        title: row.title,
        domain: row.domain,
        status: row.status
      }
      websiteApi.setStatus(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 新增
    goAdd(val) {
      console.log(val)
      this.id = ''
      this.addForm = {}
      this.addForm.parent_id = val ? val.id : 0
      this.addForm.status = 1
      console.log(this.addForm)
      this.dialogShow = true
    },
    // 切换站点
    changeParent(val) {
      console.log(val, '===============')
      this.$forceUpdate()
    },
    // 编辑
    editor(row) {
      // this.id = row.id
      this.addForm = {
        title: row.title,
        domain: row.domain,
        status: row.status,
        id: row.id,
        parent_id: row.parent_id
      }
      this.dialogShow = true
    },
    submit() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          if (!this.addForm.id) {
            websiteApi.websiteSave({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getWebList()
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            websiteApi.websiteUpdate({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getWebList()
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },
    getWebList() {
      websiteApi.websiteList().then(res => {
        // 新增后的站点列表更新到vuex中
        this.$store.commit('tagsView/WEBLIST', res.data)
      })
    },
    // 查看模型字段列表
    check(row) {
      this.$router.push({ name: 'modelField', query: { id: row.id } })
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该站点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          websiteApi.websiteDelete({ id: row.id }).then(res => {
            if (res.code == 0) {
              this.$message.success('已删除')
              this.getList()
            } else {
              this.$message.error('删除失败')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 搜索
    search(searchForm) {
      this.$refs.searchForm.validate(valid => {
        this.page.currentPage = 1
        this.getList(this.val)
      })
    },
    // 重置搜索框
    reset(searchForm) {
      this.searchForm.title = ''
    },
    // 站点列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      // this.val.title = this.searchForm.title
      websiteApi.websiteList(this.val).then(res => {
        this.orderData = res.data
        this.page.total = res.count
      })
    },

    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss">
#order {
  .link {
    .el-select {
      width: 100%;
    }
  }
  .addForm .el-form-item__content {
    width: calc(100% - 80px);
  }
}
</style>
<style scoped lang="scss">
.el-table {
  margin-bottom: 20px;
}
.search {
  min-width: 1350px;
  background-color: #fff;
  padding: 35px 30px 20px;
  border-radius: 5px;
}
.el-form-item {
  width: 100%;
}
</style>
